﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Mohe.Scada.Entities.Models;
using Mohe.Scada.Util.Models;
using Mohe.Scada.WebApi.Services;
using OpcUaHelper;
using System.Security.Cryptography;

namespace Mohe.Scada.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoadClientController : ControllerBase
    {
        private readonly IFreeSql freeSql;
        private readonly LoadServer _server;
        public LoadClientController(IFreeSql appDb, LoadServer server)
        {
            freeSql = appDb;
            _server = server;
        }
        [HttpGet("GetRealTime/{stNo}")]
        public ApiResult GetRealTime(int stNo)
        {
            return ApiResult.Success(_server.GetRealTime(stNo));
        }

        [HttpPost("Command")]
        public ApiResult Command(Cmd cmdr)
        {
            return ApiResult.Success(_server.DoCommand(cmdr));
        }
        [HttpPost("InsertCommandRecord")]
        public ApiResult InsertCommandRecord(CommandRecord cmdr)
        {
            bool ret = freeSql.Insert(cmdr).ExecuteAffrows() > 0;
            return ApiResult.Success(ret);
        }
        [HttpGet("GetLoadReportListByDate")]
        public ApiResult GetLoadReportListByDate(DateTime startTime, DateTime endTime)
        {
            var list = freeSql.Select<LoadReport>().Where(a => a.InsertTime >= startTime && a.InsertTime <= endTime).ToList();
            return ApiResult.Success(list);
        }
        [HttpGet("GetLoadReportList")]
        public ApiResult GetLoadReportList()
        {
            var list = freeSql.Queryable<LoadReport>().ToList();
            return ApiResult.Success(list);
        }
        [HttpGet("GetLoadReportListByFbarID/{fbarId}")]
        public ApiResult GetLoadReportListByFbarID(int fbarId)
        {
            var list = freeSql.Select<LoadReport>().Where(a => a.FbarId == fbarId).ToList();
            return ApiResult.Success(list);
        }

        [HttpGet("GetLoadReportListBySerial/{serial}")]
        public ApiResult GetLoadReportListBySerial(int serial)
        {
            var item = freeSql.Select<LoadReport>().Where(a => a.ProductSerial == serial).First();
            return ApiResult.Success(item);
        }

        [HttpGet("GetProductList")]
        public ApiResult GetProductList()
        {
            var list = freeSql.Queryable<Product>().ToList();
            return ApiResult.Success(list);
        }

        [HttpGet("GetProductListVisable")]
        public ApiResult GetProductListVisable()
        {
            var list = freeSql.Select<Product>().
                 Where(p => p.View == true && p.UseFlag == true).
                 ToList();
            return ApiResult.Success(list);
        }
        [HttpGet("GetLoadInfoList/{stNo}")]
        public ApiResult GetLoadInfoList(int stNo)
        {
            var list = freeSql.Select<LoadInfo>().Where(a => a.BelongsID == stNo).ToList();
            return ApiResult.Success(list);
        }

        [HttpGet("GetProductByPID/{pid}")]
        public ApiResult GetProductByPID(int pid)
        {
            Product pp = freeSql.Select<Product>().Where(p => p.Pid == pid).ToOne();
            return ApiResult.Success(pp);
        }
        [HttpGet("GetLoadReportMaxSerial")]
        public ApiResult GetLoadReportMaxSerial()
        {
            int serial = freeSql.Select<LoadReport>().Max(x => x.ProductSerial);
            return ApiResult.Success(serial);
        }
        [HttpPost("InsertLoadReport")]
        public ApiResult InsertLoadReport(LoadReport loadReport)
        {
            var ret = freeSql.Insert(loadReport).ExecuteAffrows();
            return ApiResult.Success(ret);
        }

    }
}
